#include "SaveWindow.h"

SaveWindow::SaveWindow(QWidget *parent) : QDialog(parent){
    setupUi(this);
    this->videoManager = VideoManagerSingleton::getInstance();
    this->videoListWidget->setCurrentRow(0);
    this->audioListWidget->setCurrentRow(0);
    this->connect(this->outFileNameButton, SIGNAL(clicked()), this, SLOT(chooseFileName()));
    this->connect(this->videoListWidget, SIGNAL(currentTextChanged(QString)), this, SLOT(videoFormatChanged(QString)));
    this->connect(this->audioListWidget, SIGNAL(currentTextChanged(QString)), this, SLOT(audioFormatChanged(QString)));
    this->connect(this->formatComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateFileName()));
    //this->connect(this, SIGNAL(accepted()), this, SLOT(save()));
}

SaveWindow::~SaveWindow(){
}

void SaveWindow::chooseFileName(){
    QString fileName = QFileDialog::getSaveFileName(this, tr("Enregistrer les extractions"), QString()); // , tr("Vidéo") + " (*.avi *.mpg *.dv) " + tr(" Audio") + " (*.mp3 *.mp2 *.ogg) all (*.*)"TODO find out why the foramts are not print as expected
    if(!fileName.isNull()){
            this->outFileNameLineEdit->setText(fileName);
    }
    this->updateFileName();
}

void SaveWindow::updateFileName(){
    if(this->formatComboBox->currentIndex()==0){
        this->videoFormatChanged(this->videoListWidget->currentItem()->text());
    }else{
        this->audioFormatChanged(this->audioListWidget->currentItem()->text());
    }
}

void SaveWindow::videoFormatChanged(QString format){
    QString fileName(this->outFileNameLineEdit->text());
    if(fileName != "" && !fileName.endsWith(format)){
        int dotIndex = fileName.lastIndexOf(".");
        if(dotIndex == -1){
            fileName += "." + format;
        }else{
            fileName = fileName.left(dotIndex) + "." + format;
        }
        this->outFileNameLineEdit->setText(fileName);
    }
}

void SaveWindow::audioFormatChanged(QString format){
    QString fileName(this->getOutFileName());
    if(fileName != "" && !fileName.endsWith(format)){
        int dotIndex = fileName.lastIndexOf(".");
        if(dotIndex == -1){
            fileName += "." + format;
        }else{
            fileName = fileName.left(dotIndex) + "." + format;
        }
        this->outFileNameLineEdit->setText(fileName);
    }
}

QString SaveWindow::getOutFileName(){
    return this->outFileNameLineEdit->text();
}

//void SaveWindow::save(){
    /*
    QProgressDialog progressDialog;
    progressDialog.setVisible(true);
    progressDialog.setLabelText(tr("Enregistrement en cours..."));
    progressDialog.setRange(0, 100);
    progressDialog.setValue(10);
    progressDialog.setWindowTitle(tr("Enregistrement"));
    progressDialog.setWindowIcon(QIcon(":/icons/application.png"));
    QCoreApplication::processEvents (QEventLoop::AllEvents);
    //*/
    //this->infoLabel->setText(tr("Encodage en cours"));
    //TODO Find out why the text isn't print on to the progressDialog at the begining
    //TODO Find out how to only let the user click on the progressDialog
    //this->videoManager->saveExtractions(this->getOutFileName());

    //progressDialog.setValue(100);
    //progressDialog.setVisible(false);
    //this->infoLabel->setText(tr("Encodage terminé"));
    //QMessageBox::information(this, tr("Enregistrement"), tr("Fichier enregistré avec succès !"));
//}
